package bb.lanxing.net.okhttp;

import android.text.TextUtils;
import androidx.collection.ArrayMap;
import bb.lanxing.net.core.ApiClientConfig;
import bb.lanxing.net.core.ApiRequest;
import bb.lanxing.net.core.ApiRequestConvert;
import java.io.File;
import java.util.Map;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

public class OkRequestConvert implements ApiRequestConvert<Request> {
    protected String getUserAgent() {
        return null;
    }

    @Override
    public Request convert(ApiRequest req, ApiClientConfig config) {
        HttpUrl.Builder newBuilder;
        Request.Builder builder = new Request.Builder();
        RequestBody requestBody = null;
        String baseUrl = config != null ? config.baseUrl() : null;
        if (baseUrl != null) {
            newBuilder = HttpUrl.parse(baseUrl).newBuilder();
            newBuilder.addPathSegments(req.url());
        } else {
            newBuilder = HttpUrl.parse(req.url()).newBuilder();
        }
        Map<String, String> queryParams = req.queryParams();
        if (queryParams != null) {
            for (Map.Entry<String, String> entry : queryParams.entrySet()) {
                newBuilder.addQueryParameter(entry.getKey(), entry.getValue());
            }
        }
        builder.url(newBuilder.build());
        String mediaType = req.mediaType();
        String lowerCase = req.method().toLowerCase();
        if (lowerCase.equals("post") || lowerCase.equals("put") || lowerCase.equals("patch") || lowerCase.equals("delete")) {
            File bodyAsFile = req.bodyAsFile();
            String bodyAsString = req.bodyAsString();
            if (bodyAsFile != null) {
                requestBody = RequestBody.create(MediaType.parse(mediaType), bodyAsFile);
            } else if (bodyAsString != null) {
                requestBody = RequestBody.create(MediaType.parse(mediaType), bodyAsString);
            } else if (req.multiPartBody() != null) {
                requestBody = req.multiPartBody();
            } else if (!lowerCase.equals("delete")) {
                throw new IllegalArgumentException("body == null");
            }
            lowerCase.hashCode();
            char c = 65535;
            switch (lowerCase.hashCode()) {
                case -1335458389:
                    if (lowerCase.equals("delete")) {
                        c = 0;
                        break;
                    }
                    break;
                case 111375:
                    if (lowerCase.equals("put")) {
                        c = 1;
                        break;
                    }
                    break;
                case 3446944:
                    if (lowerCase.equals("post")) {
                        c = 2;
                        break;
                    }
                    break;
                case 106438728:
                    if (lowerCase.equals("patch")) {
                        c = 3;
                        break;
                    }
                    break;
            }
            switch (c) {
                case 0:
                    if (requestBody == null) {
                        builder.delete();
                        break;
                    } else {
                        builder.delete(requestBody);
                        break;
                    }
                case 1:
                    builder.put(requestBody);
                    break;
                case 2:
                    builder.post(requestBody);
                    break;
                case 3:
                    builder.patch(requestBody);
                    break;
            }
        } else if (lowerCase.equals("get")) {
            builder.get();
        }
        ArrayMap arrayMap = new ArrayMap();
        String userAgent = getUserAgent();
        if (!TextUtils.isEmpty(userAgent)) {
            arrayMap.put("User-Agent", userAgent);
        }
        if (req.headers() != null) {
            arrayMap.putAll(req.headers());
        }
        builder.headers(Headers.of(arrayMap));
        return builder.build();
    }
}
